﻿<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="java.sql.*" %>
<%
//http://127.0.0.1:8080/PushServer/ThirdPartPush.jsp?devicetoken=APA91bGMn2pdGbTM040EkYJfe24wragQnuepQVKLFPslcAlh0ZdOJNye87hJbNbsYhjFYHY6kGLi4-2Tyec6pxOKHQ7mByhWPCv_l1rqv9J2AF5rjJGD9nE&devicename=android&usercode=12&tokenid=12111111&svrID=A001&phonenumber=01024832467&cmd=I
%>
<%
////////////////DB OPEN ////////////////////////////////////
///////////////////////////////////////////////////////////
Connection conn = null;

try {
	Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
	conn = DriverManager.getConnection ("jdbc:sqlserver://203.225.3.188:1433;DatabaseName=PushServer", "pushserver", "pushserver");
} catch (ClassNotFoundException e) {
	  
	System.out.println("connectDB() 상태 업데이트 DB 접속 오류"+ e.toString());
		throw e;
} catch (SQLException e) {
	System.out.println("connectDB() 상태 업데이트 DB 접속 오류"+ e.toString());

	throw e;
}
///////////////////////////////////////////////////////////

        System.out.println("###################################################################################");
        System.out.println("######################################>> ThirdPartPush.jsp Start<<#################");
        System.out.println("###################################################################################");
	
        //처리구분(I:가입, D:해지)
	String cmd = request.getParameter("cmd");
	if(cmd == null) {
		cmd = "I";
	}

	String svrId 	    = request.getParameter("svrID");       // 서비스ID(앱관리 번호)
	String devicetoken  = request.getParameter("devicetoken"); //장치 토큰정보
	String devicename   = request.getParameter("devicename");  //장치명(android, iphone)
	String phonenumber  = request.getParameter("phonenumber"); //휴대폰 번호
	String tokenid 		= request.getParameter("tokenid"); 	   //토큰아이디(아이폰 사용)
	String usercode 	= request.getParameter("usercode");   //usercode(아이폰 사용)
	String userid = request.getParameter("userid");
		
	if(devicetoken == null) return;
	if(devicename.toLowerCase().equals("android")){
		if(phonenumber == null) return;
	}

	System.out.println(">> svrId:"+svrId);
        System.out.println(">> devicetoken:"+devicetoken);
        System.out.println(">> phonenumber:"+phonenumber);
        System.out.println(">> devicename:"+devicename);

	
	//가입 처리 
	if(cmd.equals("I")) {
		//폰번호 체크
		//if(phonenumber != null){
			
		if(devicename.toLowerCase().equals("android")){
			//안드로이드 경우 삭제 	
			PreparedStatement pstmt=conn.prepareStatement("DELETE FROM CMC_MOBILE WHERE PHONENUMBER =? AND SVRID =? ");
 			pstmt.setString(1,phonenumber);
 			pstmt.setString(2,svrId);
 			pstmt.execute();
 			if(pstmt!=null) pstmt.close();
		}else{
			//아이폰 계열
			PreparedStatement pstmt=conn.prepareStatement("DELETE FROM CMC_MOBILE WHERE TOKENID =? AND SVRID =? ");
	 		pstmt.setString(1,devicetoken);
	 		pstmt.setString(2,svrId);
	 		pstmt.execute();
	 		if(pstmt!=null) pstmt.close();
		}
		//}
		
		//장치 토큰정보 입력
		 PreparedStatement pstmt=conn.prepareStatement("INSERT INTO CMC_MOBILE (DEVICEID, DEVICENAME, USERCODE, TOKENID, SVRID, PHONENUMBER) VALUES(?, ?, ?, ?, ?, ?)");
		 pstmt.setString(1,"");
		 pstmt.setString(2,devicename);
		 pstmt.setString(3,userid);
		 pstmt.setString(4,devicetoken);
		 pstmt.setString(5,svrId);
		 pstmt.setString(6,phonenumber);
 		 pstmt.execute();
 		 if(pstmt!=null) pstmt.close();
		
	}else if(cmd.equals("D")) {
		//폰번호 체크
		//if(phonenumber != null){
		if(devicename.toLowerCase().equals("android")){
			//안드로이드 경우 삭제 	
			PreparedStatement pstmt=conn.prepareStatement("DELETE FROM CMC_MOBILE WHERE PHONENUMBER =? AND SVRID =? ");
 			pstmt.setString(1,phonenumber);
 			pstmt.setString(2,svrId);
 			pstmt.execute();
 			if(pstmt!=null) pstmt.close();
		}else{
			//아이폰 계열
			PreparedStatement pstmt=conn.prepareStatement("DELETE FROM CMC_MOBILE WHERE TOKENID =? AND SVRID =? ");
	 		pstmt.setString(1,tokenid);
	 		pstmt.setString(2,svrId);
	 		pstmt.execute();
	 		if(pstmt!=null) pstmt.close();
		}
		//}		
	}
	
        System.out.println("###################################################################################");
        System.out.println("######################################>> ThirdPartPush.jsp End<<###################");
        System.out.println("###################################################################################");

////////////////////// DB CLOSE ////////////////////////////
///////////////////////////////////////////////////////////
if(conn != null) {
	try {
		conn.close();
	} catch (Exception e) {
	}
}
conn = null;
///////////////////////////////////////////////////////////
%>
OK